//
//  LZFTuiJianVC.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/21.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFTuiJianVC.h"
#import "LZFTuiJianItem.h"
#import "LZFTuiJianCell.h"
#import <AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD.h>

@interface LZFTuiJianVC ()
@property(nonatomic,strong)NSMutableArray *tuiJianItemArray;
@property(nonatomic,weak) NSURLSessionDataTask *dataTask;
@end

/*
 需求: 
 请求网络数据
 等高cell的数据展示
 全屏分割线的设置

 */
@implementation LZFTuiJianVC

-(NSMutableArray *)tuiJianItemArray
{
    if (_tuiJianItemArray==nil) {
        _tuiJianItemArray=[NSMutableArray new];
    }
    
    return _tuiJianItemArray;
}


static NSString *ID=@"tuijian";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置控制器的标题
    self.title=@"推荐标签";

    //发送网络请求加载数据
    [self loadData];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LZFTuiJianCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
    //设置tableview行高
    self.tableView.rowHeight=100;
    
    //设置背景颜色
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor=LZFColor(215, 215, 215);
}

//tuijian

-(void)viewWillDisappear:(BOOL)animated
{
    [_dataTask cancel];
    [SVProgressHUD dismiss];

    
}

-(void)loadData
{
        //http://api.budejie.com/api/api_open.php?a=tag_recommend&action=sub&c=topic
    
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    
    NSMutableDictionary *dic =[NSMutableDictionary new];
    dic[@"a"]=@"tag_recommend";
     dic[@"action"]=@"sub";
     dic[@"c"]=@"topic";
    
    //指示器
    [SVProgressHUD showWithStatus:@"正在加载数据...."];
    
    
   _dataTask= [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",[NSThread currentThread]);
//        NSLog(@"%@",[responseObject class]);
       
       //数据加载完成后,取消指示器
       [SVProgressHUD dismiss];
          self.tuiJianItemArray =  [LZFTuiJianItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];

        NSLog(@"%@",error);
        
    }];
    
    
    
}


#pragma mark tableview datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.tuiJianItemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZFTuiJianItem *tuiJianItem=self.tuiJianItemArray[indexPath.row];
    
    LZFTuiJianCell *tuiJianCell=[tableView  dequeueReusableCellWithIdentifier:ID];
    
    tuiJianCell.tuijianItem=tuiJianItem;
    
    return  tuiJianCell;
    
}


@end
